package cn.flant.cmd;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class ParamContent {
	private Map<String, Object> paramMap = new HashMap<String, Object>();

	public Set<String> getParamKeys() {
		return new HashSet<String>(paramMap.keySet());
	}

	public void clearParam() {
		paramMap.clear();
	}

	public boolean hasParamName(String name) {
		return paramMap.containsKey(name);
	}

	public boolean hasParamValue(String name) {
		return paramMap.get(name) != null;
	}

	public boolean hasValidValue(String name) {
		Object value = paramMap.get(name);
		if (value == null) {
			return false;
		} else if (value instanceof String) {
			return ((String) value).trim().length() > 0;
		} else {
			return true;
		}
	}

	public ParamContent putParam(String name, Object value) {
		paramMap.put(name, value);
		return this;
	}

	public Object removeParam(String name) {
		return paramMap.remove(name);
	}

	public Object getParam(String name, Object deft) {
		Object value = paramMap.get(name);
		if (value == null) {
			return deft;
		} else {
			return value;
		}
	}

	public Object getParam(String name) {
		return paramMap.get(name);
	}

	public String getStringParam(String name, String deft) {
		Object obj = paramMap.get(name);
		if (obj == null) {
			return deft;
		}
		return obj.toString();
	}

	public String getStringParam(String name) {
		return getStringParam(name, null);
	}

	public Integer getIntegerParam(String name, Integer deft) {
		Object obj = paramMap.get(name);
		if (obj == null) {
			return deft;
		} else if (obj instanceof Integer) {
			return (Integer) obj;
		} else if (obj instanceof Long) {
			Long longValue = (Long) obj;
			return longValue.intValue();
		} else {
			String str = obj.toString();
			try {
				return Integer.valueOf(str);
			} catch (Exception e) {
				return deft;
			}
		}
	}

	public Integer getIntegerParam(String name) {
		return getIntegerParam(name, null);
	}

	public Long getLongParam(String name, Long deft) {
		Object obj = paramMap.get(name);
		if (obj == null) {
			return deft;
		} else if (obj instanceof Integer) {
			return ((Integer) obj).longValue();
		} else if (obj instanceof Long) {
			return (Long) obj;
		} else {
			String str = obj.toString();
			try {
				return Long.valueOf(str);
			} catch (Exception e) {
				return deft;
			}
		}
	}

	public Long getLongParam(String name) {
		return getLongParam(name, null);
	}

	public Float getFloatParam(String name, Float deft) {
		Object obj = paramMap.get(name);
		if (obj == null) {
			return deft;
		} else if (obj instanceof Float) {
			return (Float) obj;
		} else if (obj instanceof Double) {
			return ((Double) obj).floatValue();
		} else if (obj instanceof Integer) {
			return ((Integer) obj).floatValue();
		} else if (obj instanceof Long) {
			return ((Long) obj).floatValue();
		} else {
			String str = obj.toString();
			try {
				return Float.valueOf(str);
			} catch (Exception e) {
				return deft;
			}
		}
	}

	public Float getFloatParam(String name) {
		return getFloatParam(name, null);
	}

	public Double getDoubleParam(String name, Double deft) {
		Object obj = paramMap.get(name);
		if (obj == null) {
			return deft;
		} else if (obj instanceof Double) {
			return (Double) obj;
		} else if (obj instanceof Float) {
			return ((Float) obj).doubleValue();
		} else if (obj instanceof Integer) {
			return ((Integer) obj).doubleValue();
		} else if (obj instanceof Long) {
			return ((Long) obj).doubleValue();
		} else {
			String str = obj.toString();
			try {
				return Double.valueOf(str);
			} catch (Exception e) {
				return deft;
			}
		}
	}

	public Double getDoubleParam(String name) {
		return getDoubleParam(name, null);
	}

	public Character getCharacterParam(String name, Character deft) {
		Object obj = paramMap.get(name);
		if (obj == null) {
			return deft;
		} else if (obj instanceof Character) {
			return (Character) obj;
		} else {
			String str = obj.toString();
			if (str.length() != 1) {
				return deft;
			}
			char[] chars = str.toCharArray();
			if (chars.length != 1) {
				return deft;
			} else {
				return chars[0];
			}
		}
	}

	public Character getCharacterParam(String name) {
		return getCharacterParam(name, null);
	}

	public Byte getByteParam(String name, Byte deft) {
		Object obj = paramMap.get(name);
		if (obj == null) {
			return deft;
		} else if (obj instanceof Byte) {
			return (Byte) obj;
		} else {
			try {
				return Byte.valueOf(obj.toString());
			} catch (Exception e) {
				return deft;
			}
		}
	}

	public Byte getByteParam(String name) {
		return getByteParam(name, null);
	}

}
